package validators;

import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import dtos.UserDTO;
import utils.UserDAORemote;

@FacesValidator("validators.EmailDuplicateValidator")
public class EmailDuplicateValidator implements Validator  {

	
	@EJB
	UserDAORemote udr;
	
	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());
	
	public EmailDuplicateValidator(){
	}
	
	/**
	 * Verifies if the email is duplicate or not
	 */
	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		UserDTO tempUser=udr.getUserDTOByEmail( (String) value);
		if(tempUser!=null)
		{
			FacesMessage message = new FacesMessage(bundle.getString("Validators_EmailDuplicateValidator_Error"));
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
		

	}
	
}
